package com.cpi.cornpp.bushelprice.services;

import java.util.ArrayList;
import java.util.List;

import com.cpi.cornpp.bushelprice.converter.BushelPriceConverter;
import com.cpi.cornpp.bushelprice.dao.BushelPriceDAO;
import com.cpi.cornpp.bushelprice.exception.BushelPriceException;
import com.cpi.cornpp.bushelprice.view.BushelPriceView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.monthsperiods.services.MonthsPeriodsService;

/**
 * Bushel Price Service
 * 
 * @author Edgar Joao
 *
 */
public class BushelPriceService {
	/**
	 * Obtiene un listado de Precio de Bushel
	 * 
	 * @author Edgar Joao	
	 * @param orderByColumn
	 * @param ascendingOrder
	 * @return
	 * @throws Exception
	 */
	public List list(int orderByColumn, boolean ascendingOrder, BushelPriceView view) throws Exception{
		BushelPriceDAO bushelPriceDAO = null;
		BushelPriceConverter bConverter = null;
		List bushelPriceDTOs = null;
		List bushelPriceViews = null;						
		try {
			bushelPriceDAO = new BushelPriceDAO();
			bushelPriceDTOs = new ArrayList(0);
			bushelPriceViews = new ArrayList(0);
			bConverter = new BushelPriceConverter();			
			bushelPriceDTOs = bushelPriceDAO.list(orderByColumn, ascendingOrder, bConverter.converterBushelPriceViewToDto(view));			
			bushelPriceViews = bConverter.convertDTOsToViews(bushelPriceDTOs);						
		} catch (BushelPriceException bushelPriceException) {
			throw bushelPriceException;
		}catch (Exception e){
			BushelPriceException bushelPriceException = null;
			e.printStackTrace(System.out);
			bushelPriceException = new BushelPriceException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw bushelPriceException;
		}					
		return bushelPriceViews;
	}
	/**
	 * Agrega un Precio de Bushel
	 * 
	 * @author Edgar Joao
	 * @param view
	 * @throws BushelPriceException
	 */
	public void insertBushelPrice(BushelPriceView view) throws BushelPriceException{
		BushelPriceDAO bushelPriceDAO = null;
		BushelPriceConverter bConverter = null;
		MonthsPeriodsService periodsService = null;
		try{
			bushelPriceDAO = new BushelPriceDAO();
			bConverter = new BushelPriceConverter();
			periodsService = new MonthsPeriodsService();
			view.setMonthPeriodId(periodsService.actualMonthPeriod());			
			bushelPriceDAO.insertBushelPrice(bConverter.converterBushelPriceViewToDto(view));
		} catch (BushelPriceException bushelPriceException) {
			throw bushelPriceException;
		}catch (Exception e){
			BushelPriceException bushelPriceException = null;
			e.printStackTrace(System.out);
			bushelPriceException = new BushelPriceException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw bushelPriceException;
		}
	}
	/**
	 * Metodo que edita un Precio de Bushel
	 * @author Edgar Joao
	 * @param dto
	 * @throws BushelPriceException
	 */
	public void editBushelPrice(BushelPriceView view) throws BushelPriceException{
		BushelPriceDAO bushelPriceDAO = null;
		BushelPriceConverter bConverter = null;		
		try{
			bushelPriceDAO = new BushelPriceDAO();
			bConverter = new BushelPriceConverter();											
			bushelPriceDAO.editBushelPrice(bConverter.converterBushelPriceViewToDto(view));
		} catch (BushelPriceException bushelPriceException) {
			throw bushelPriceException;
		}catch (Exception e){
			BushelPriceException bushelPriceException = null;
			e.printStackTrace(System.out);
			bushelPriceException = new BushelPriceException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw bushelPriceException;
		}
	}
	/**
	 * Metodo que obtiene un Precio Bushel por Id
	 * 
	 * @author Edgar Joao
	 * @param bushelPriceId
	 * @return
	 * @throws BushelPriceException
	 */
	public BushelPriceView getBushelPriceView(int bushelPriceId) throws BushelPriceException{
		BushelPriceDAO bushelPriceDAO = null;
		BushelPriceConverter bConverter = null;
		BushelPriceView view = null;
		try{
			bushelPriceDAO = new BushelPriceDAO();
			bConverter = new BushelPriceConverter();
			view = bConverter.converterBushelPriceDtoToView(bushelPriceDAO.getBushelPriceDTO(bushelPriceId));
		} catch (BushelPriceException bushelPriceException) {
			throw bushelPriceException;
		}catch (Exception e){
			BushelPriceException bushelPriceException = null;
			e.printStackTrace(System.out);
			bushelPriceException = new BushelPriceException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw bushelPriceException;
		}
		return view;
	}
}
